import 'dart:async';
import 'package:geolocator/geolocator.dart';
import 'package:permission_handler/permission_handler.dart';

class LocationService {
  StreamSubscription? _positionSubscription;

  Stream<Position> getPositionStream() async* {
    var status = await Permission.location.request();
    if (status.isGranted) {
      const locationSettings = LocationSettings(
        accuracy: LocationAccuracy.high,
        distanceFilter: 0,
      );
      yield* Geolocator.getPositionStream(locationSettings: locationSettings);
    } else {
      throw Exception('Location permission denied');
    }
  }

  void stopLocationUpdates() {
    _positionSubscription?.cancel();
    _positionSubscription = null;
  }
}